package com.edz.dudumifi.event;

import android.content.Context;
import android.util.Log;

import com.edawtech.module.Constants;
import com.edawtech.module.LoginMsg;
import com.edz.NettyClientBootstrap;
import com.edz.dudumifi.base.Config;
import com.edz.dudumifi.utils.PreferencesUtils;

/**
 * Created by ganshh.gan on 2018/5/16.
 */
public class NettyClientConnectionThread extends Thread {
    private static final String TAG = "ConnectionThread";
    private Context context;

    public NettyClientConnectionThread(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        super.run();

        try {
            startConnection();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void startConnection() throws InterruptedException {
        //确保imei 已经获取
        String imei = null;
        while (true) {
            imei = PreferencesUtils.getString(context, Config.IMEI);

            if (imei != null)
                break;
        }

        Log.i(TAG, "imei" + imei);

        Constants.setClientId(imei); //替换成imei
        //NettyClientBootstrap bootstrap=new NettyClientBootstrap(9999,"192.168.0.33");

        //先向服务器发起登录
        LoginMsg loginMsg = new LoginMsg();
        loginMsg.setPassword("atom");
        loginMsg.setUserName("atom");

        NettyClientBootstrap.getInstance(Config.server_port, Config.server_address).getSocketChannel().writeAndFlush(loginMsg);

    }
}
